﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Ledger.aspx.cs" Inherits="CRMWeb.Pages.AM.Ledger" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <div style="float: left; width: 100%">
       
        <table id="dgLedger" title="LEDGER" class="easyui-datagrid" style="width: auto; height: 300px"
            data-options="url:'/WebServices/AM/Ledger.ashx?method=LedgerSelect&LoanID='+getQuerystring('LoanID', ''),pageSize:10 " pagination="true"
            rownumbers="true" fitcolumns="true" singleselect="true">
            <thead>
                <tr>
                    <th field="date" width="60" data-options="formatter:formatDate">Date</th>
                    <th field="description" width="50">Transactions</th>
                    <th field="debit" width="50">Debit</th>
                    <th field="credit" width="50">Credit</th>
                    <%--<th field="amount" width="50">Repayment</th>--%>
                    <th field="balance" width="50">Balance</th>
                </tr>
            </thead>
        </table>
    </div>
      
    <script type="text/javascript">

        $(document).ready(function () {
            //fnLeftNavSelected('lnkLedger');
            $('#dgLedger').datagrid({
                onDblClickRow: function (index, rowdata) {
                    editLedger();
                }
            });

        });

        var url;
        function newLedger() {
            $('#dlgLedger').dialog('open').dialog('setTitle', 'New Ledger');
            $('#fmLedger').form('clear');
            $('#txtDate').datebox('setValue', null);
            $('#txtTransactionsCharge').val('');
            //$('#txtRepayment').numberbox('setValue', '0.00');
            $("#rdoDebit").prop("checked", true);
            $('#txtClosingBalance').numberbox('setValue', '0.00');
            $('#txtDCAmount').numberbox('setValue', '0.00');
            $('#hdnLedgerID').val('');
            getClosingBalance();
            url = '/WebServices/AM/Ledger/LedgerAdd';
        }

        function getClosingBalance() {
            $.ajax({
                url: '/WebServices/AM/Ledger.ashx?method=getClosingBalance',
                contentType: 'application/json; charset=utf-8',
                data: { 'LoanID': getQuerystring('LoanID', '') },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, rec) {
                        /* set value in labels */
                        $('#txtRepayment').numberbox('setValue', rec.balance);
                    });
                }
            });
        }

        function editLedger() {
            var row = $('#dgLedger').datagrid('getSelected');
            if (row) {
                $('#dlgLedger').dialog('open').dialog('setTitle', 'Edit Ledger');
                $('#fmLedger').form('load', row);
                $('#txtDate').datebox('setValue', formatDate(row.date, null));
                $('#txtTransactionsCharge').val(row.description);
                $('#txtRepayment').numberbox('setValue', row.amount);
                if (row.debit != 0.00) {
                    $('#txtDCAmount').numberbox('setValue', row.debit);
                    $("#rdoDebit").prop("checked", true);
                }
                else {
                    $('#txtDCAmount').numberbox('setValue', row.credit);
                    $("#rdoCredit").prop("checked", true);
                }
                $('#txtClosingBalance').numberbox('setValue', row.balance);
                $('#hdnLedgerID').val(row.ledgerid); 
                url = '/WebServices/AM/Ledger/LedgerEdit?ledgerid=' + row.ledgerid;
            }
        }

        function saveLedger() {
            var TransactionsCharge = $('#txtTransactionsCharge').val();
            var Repayment1 = $("#txtRepayment").numberbox('getValue');
            var ClosingBalance = $("#txtClosingBalance").numberbox('getValue');
            var LedgerDate = $('#txtDate').datebox('getValue');
            if (LedgerDate != '')
                LedgerDate = DefaultformatLoanDate($('#txtDate').datebox('getValue'));
            if (Repayment1 == '')
                Repayment1 = '0.00';
            var ResultID = $('#hdnLedgerID').val();
            var DebitAmount;
            var CreditAmount;

            if (($('#rdoDebit:checked').val()) == "on") {
                DebitAmount = $("#txtDCAmount").numberbox('getValue');
                CreditAmount = '0.00';
            }
            else {
                CreditAmount = $("#txtDCAmount").numberbox('getValue');
                DebitAmount = '0.00';
            }
            if (ResultID != '') {
                if (LedgerDate != '') {
                    $.ajax({
                        url: '/WebServices/AM/Ledger.ashx',
                        type: 'POST',   // I'm doing a POST here just to show it can handle it too... 
                        data: {
                            method: 'LedgerEdit',
                            args: { ledgerid: ResultID, LedgerDate: LedgerDate, TransactionsCharge: TransactionsCharge, Repayment1: Repayment1, DebitAmount: DebitAmount, CreditAmount: CreditAmount, ClosingBalance: ClosingBalance }
                        },
                        success: function (data) {
                            if (data == 1) {
                                $('#dlgLedger').dialog('close');      // close the dialog  
                                $('#dgLedger').datagrid('reload');    // reload the user data  
                                alert('Update Data Successfully.');
                            }
                            else {
                                alert('Ledger already exist.');
                            }
                        }
                    });
                }
            }
            else {
                if (LedgerDate != '') {
                    $.ajax({
                        url: '/WebServices/AM/Ledger.ashx',
                        type: 'POST',   // I'm doing a POST here just to show it can handle it too... 
                        data: {
                            method: 'AddLedger',
                            args: { LedgerDate: LedgerDate, TransactionsCharge: TransactionsCharge, Repayment1: Repayment1, DebitAmount: DebitAmount, CreditAmount: CreditAmount, ClosingBalance: ClosingBalance, LoanID: getQuerystring('LoanID', '') }
                        },
                        success: function (data) {
                            if (data == 1) {
                                $('#dlgLedger').dialog('close');      // close the dialog  
                                $('#dgLedger').datagrid('reload');    // reload the user data  
                                alert('Insert Data Successfully.');
                            }
                            else {
                                alert('Ledger already exist.');
                            }
                        }
                    });
                }
            }
        }

        function deleteLedger() {
            var row = $('#dgLedger').datagrid('getSelected');
            if (row) {
                $.ajax({
                    url: '/WebServices/AM/Ledger.ashx',
                    type: 'POST',   // I'm doing a POST here just to show it can handle it too... 
                    data: {
                        method: 'LedgerDelete',
                        args: { ledgerid: row.ledgerid }
                    },
                    success: function (data) {
                        $('#dgLedger').datagrid('reload');
                        alert('Delete Data Successfully.');
                    }
                });
            }
        }

        function formatDate(value, row) {
            if (value == null)
                return value;
            var date = new Date(value);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return (d < 10 ? ('0' + d) : d) + '/' + (m < 10 ? ('0' + m) : m) + '/' + y;
        }

        function Print() {
            w = window.open();
            w.document.write($('#Printdata').html());
            w.print();
            w.close();
        }

        function GenerateLedger() {
            $.ajax({
                url: '/WebServices/AM/Ledger.ashx',
                type: 'POST',   // I'm doing a POST here just to show it can handle it too... 
                data: {
                    method: 'GenerateLadger',
                    args: { LoanID: getQuerystring('LoanID', '') }
                },
                success: function (result) {
                    $("#Printdata").html(result);
                }
            });
        }
      
        function CalculateClosing() {
            var ClosingBalance;
            if (($('#rdoDebit:checked').val()) == "on") {
                var ClosingBalance = parseFloat($("#txtRepayment").numberbox('getValue')) + parseFloat($("#txtDCAmount").numberbox('getValue'));
                $('#txtClosingBalance').numberbox('setValue', ClosingBalance);
            }
            else {
                var ClosingBalance = parseFloat($("#txtRepayment").numberbox('getValue')) - parseFloat($("#txtDCAmount").numberbox('getValue'));
                $('#txtClosingBalance').numberbox('setValue', ClosingBalance);
            }
        }
    </script>
</body>
</html>
